package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 图片及背景文件上传处理器
 */
@RestController
@RequestMapping("/images")
public class ImageController {
    /**
     * 图片上传 保存
     * CKeditor富文本插件
     * @param upload 前端传入的文件流
     * @return
     */
    @PostMapping
    public Map upload(MultipartFile upload) {//参数名upload用于接收前端传入文件流，如果前端使用editor，则此参数名必须是file
        HashMap<String, Object> map = new HashMap<>();
        try {
            //String url = UploadUtil.upload(upload, UploadUtil.PATH + "/upload/");
            String url = UploadUtil.uploadQiniu(upload);
            map.put("uploaded", 1);
            map.put("url", url);
        } catch (Exception e) {
            //保存失败
            e.printStackTrace();
            map.put("uploaded", 0);
            HashMap<String, String> temp = new HashMap<>();
            temp.put("message", "上传失败！");
            map.put("error", temp);
        }
        return map;
    }
}
